﻿using System;
using System.Windows.Forms;
using BGShell.Forms;
using System.Drawing;
using System.Collections.Generic;

namespace BGShell
{
    internal sealed class BGShellNotifyIcon : IDisposable
    {
        private sealed class NotifyIconMenuItemOpen : ToolStripMenuItem
        {
            public NotifyIconMenuItemOpen()
            {
                Font = new Font(Font, FontStyle.Bold);
                Click += ToggleBackgroundWindow;
            }

            public override string Text
            {
                get
                {
                    if (IsBackgroundWindowVisible)
                    {
                        return Resources.NotifyIconHideText;
                    }

                    return Resources.NotifyIconOpenText;
                }
                set
                {
                }
            }

            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    Font.Dispose();
                }

                base.Dispose(disposing);
            }
        }

        private readonly NotifyIcon _icon;
        private readonly ContextMenuStrip _menu;

        private List<ToolStripMenuItem> _addInItems;

        public BGShellNotifyIcon()
        {
            _menu = new ContextMenuStrip()
            {
                Items = 
                { 
                    new NotifyIconMenuItemOpen(), 
                    "-", 
                    new ToolStripMenuItem(Resources.Exit, null, delegate
                    {
                        Application.Exit();
                    }),
                }
            };

            _icon = new NotifyIcon()
            {
                Icon = ResourcesNative.BGShellIcon,
                Text = Resources.NotifyIconText,
                ContextMenuStrip = _menu,
            };
            _icon.DoubleClick += ToggleBackgroundWindow;
        }

        public void Dispose()
        {
            _icon.Dispose();
            _menu.Dispose();
        }

        public bool Visible
        {
            get { return _icon.Visible; }
            set { _icon.Visible = value; }
        }

        internal void AddAddinCommand(String text, EventHandler onClick)
        {
            if (_addInItems == null)
            {
                _addInItems = new List<ToolStripMenuItem>();
            }

            var item = new ToolStripMenuItem(text, null, onClick);

            _addInItems.Add(item);
            _menu.Items.Insert(_addInItems.Count, item);
        }

        internal static bool IsBackgroundWindowVisible
        {
            get
            {
                var window = BGShellApplication.Current.Windows.BackgroundWindow;

                if (window != null)
                {
                    return window.Visible;
                }

                return false;
            }
        }

        internal static void ToggleBackgroundWindow(Object sender, EventArgs e)
        {
            var window = BGShellApplication.Current.Windows.BackgroundWindow;

            if (window != null)
            {
                if (IsBackgroundWindowVisible)
                {
                    window.Hide();
                }
                else
                {
                    BGShellApplication.Current.ActivateBackgroundWindow();
                }
            }
        }
    }
}
